import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/parameters.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:doctor_prescribe_lib/doctor_prescribe_component.dart';
import 'package:doctor_prescribe_lib/offline/basic_info/bloc.dart';
import 'package:doctor_prescribe_lib/western_medicine/prescription_page/bloc.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:common_lib/patient_management/patient_list/patient_list_entity.dart'
    as PatientBean;
import '../../diagnostic_result/entity/diagnostic_result_list_entity.dart'
    as DiagnosticBean;
import 'package:doctor_prescribe_lib/western_medicine/medical_search/entity/medical_search_entity.dart'
    as MedicineSearchBean;

import '../../prescribe_util.dart';

///处方开立页
class PrescriptionPage
    extends BaseBlocPage<PrescriptionPageBloc, _PrescriptionPageState> {
  PrescriptionPage(Parameters parameters) : super(parameters);

  @override
  _PrescriptionPageState generateThemeState() {
    return _PrescriptionPageState();
  }
}

class _PrescriptionPageState extends BaseBlocPageState<PrescriptionPageBloc> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            Routers.pop(context);
          },
        ),
        centerTitle: true,
        title: Text('处方开立页'),
      ),
      body: Container(
        child: StreamBuilder(
          builder: (context, snapshot) {
            return Column(
              children: [
                Expanded(
                  child: StreamBuilder(
                    stream: bloc.listStream.stream,
                    builder: (context,
                        AsyncSnapshot<List<MedicineSearchBean.Datas>>
                            snapshot) {
                      int total = (snapshot?.data?.length ?? 0) + 1;
                      return ListView.builder(
                          itemCount: total,
                          itemBuilder: (context, pos) {
                            return _buildItem(pos, theme, total, snapshot.data);
                          });
                    },
                  ),
                ),
                StreamBuilder<bool>(
                    stream: bloc.loadingStatusStream.stream,
                    builder: (context, snapshot) {
                      bool flag = snapshot.hasData ? snapshot.data : false;

                      return flag
                          ? buildLoadingIndicator()
                          : ArgonButton(
                              height: 45,
                              width: ScreenAdapter.screenWidthDp * 0.68,
                              borderRadius: 15,
                              onTap: (startLoading, stopLoading, btnState) {
                                if (btnState == ArgonButtonState.Idle) {
                                  bloc.doNext(context);
                                }
                              },
                              child: Text(
                                "下一步",
                                style: TextStyle(color: Colors.white),
                              ));
                    }),
                30.vGapDp,
              ],
            );
          },
        ),
      ),
    );
  }

  @override
  PrescriptionPageBloc generateBloc() {
    return PrescriptionPageBloc();
  }

  Widget _buildItem(
      int pos, MTheme theme, int total, List<MedicineSearchBean.Datas> list) {
    if (pos == total - 1) {
      return _buildAddItem(pos, theme);
    }

    var item = list[pos];
    return Container(
      margin: EdgeInsets.only(left: 10, top: 10, right: 10),
      padding: EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Column(
        children: [
          Row(
            children: [
              Expanded(
                  child: Text(
                item.medicineName,
                style: TextStyle(color: Colors.black, fontSize: 16),
              )),
              10.hGapDp,
              Text(
                '¥${PrescribeUtil.formatMoney(item.unitPrice)}',
                style: TextStyle(color: Colors.red, fontSize: 12),
              ),
            ],
          ),
          10.vGapDp,
          Row(
            children: [
              Expanded(
                child: Text(
                  '用法用量:${item.takeDose}${item.singleUnit.dictName}/次，${item.treat.dictName}，${item.treatRoute.dictName}',
                  style: TextStyle(fontSize: 12, color: Colors.grey),
                ),
              ),
              Text(
                'x${item.medicineAmount}',
                style: TextStyle(fontSize: 12, color: Colors.grey),
              )
            ],
          ),
          5.vGapDp,
          1.gapH(Colors.grey[300]),
          5.vGapDp,
          Row(
            children: [
              Spacer(),
              FlatButton(
                  onPressed: () {
                    Custom_iOS_Dialog.prompt(context,
                        content: '确定删除  ${item.medicineName}  ?', okFn: () {
                      bloc.doDelete(context, item);
                    });
                  },
                  child: Row(
                    children: [
                      Icon(
                        Icons.delete,
                        color: Colors.grey,
                      ),
                      Text(
                        '删除',
                        style: TextStyle(color: Colors.grey),
                      )
                    ],
                  )),
              FlatButton(
                  onPressed: () {
                    bloc.doEdit(context, item);
                  },
                  child: Row(
                    children: [
                      Icon(
                        Icons.edit,
                        color: Colors.grey,
                      ),
                      Text(
                        '编辑',
                        style: TextStyle(color: Colors.grey),
                      )
                    ],
                  )),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildAddItem(int pos, MTheme theme) {
    return Container(
      margin: EdgeInsets.only(left: 10, top: 10, right: 10),
      padding: EdgeInsets.symmetric(
          horizontal: ScreenAdapter.screenWidthDp / 5, vertical: 30),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
      ),
      child: FlatButton(
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          color: theme.themeColor.themeColor,
          onPressed: () {
            bloc.jump2MedicalSearch(context);
          },
          child: Row(
            children: [
              Spacer(),
              Icon(
                Icons.add,
                color: Colors.white,
              ),
              Text(
                '添加药品',
                style: TextStyle(color: Colors.white),
              ),
              Spacer(),
            ],
          )),
    );
  }
}
